//
//  FlightAddressController.m
//  ELife
//
//  Created by xiaozhi on 11-7-17.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "FlightAddressController.h"
#import "UIViewUtil.h"
#import "ELifeAppDelegate.h"
#import "GDataXMLNode.h"
#import "GTMBase64.h"
#import "GetMyData.h"
#import "LoginController.h"
#import "FlightSearchController.h"

@implementation FlightAddressController
@synthesize nameTextField, phoneTextField, addressTextField, pCodeTextField;


- (id) initWithAddressModel:(FlightAddressModel *) addressModel
                 withTarget:(NSObject *)target 
             withBackAction:(SEL)backAction
{
    self = [super initWithNibName:@"FlightAddressController" bundle:nil];
    if (self) {
        [_addressModel release];
        _addressModel = [addressModel retain];
        
        [_target release];
        _target = [target retain];
        
        _backAction = backAction;
    }
    
    return self;
}


- (void)dealloc
{
    [nameTextField release];
    [phoneTextField release];
    [addressTextField release];
    [pCodeTextField release];
    [_target release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIViewUtil getCommonBg];
    [UIViewUtil viewRoundFrame:[self.view viewWithTag:1]];
    // Do any additional setup after loading the view from its nib.
    
    if (_addressModel != nil) {
        [nameTextField setText:_addressModel.name];
        [phoneTextField setText:_addressModel.phone];
        [addressTextField setText:_addressModel.address];
        [pCodeTextField setText:_addressModel.pCode];
    }
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - network
- (void)requestMailId
{
    NSString* userid = [GetMyData getUserId];
    
    if (userid == nil) {
        LoginController* lc = [[LoginController alloc] init:self];
        [self.navigationController pushViewController:lc animated:YES];
        [lc release];
        return;
    }
    
    [[ELifeAppDelegate App] showProgressDialog:@"请稍后"];

    
    NSString* host =  [[NSBundle mainBundle] localizedStringForKey:(@"BussinessHost") value:@"" table:nil];
    NSURL* url = [[NSURL alloc] initWithString:[host stringByAppendingString:@"mailing_address.php"]];
    
    NSMutableDictionary *param = [[NSMutableDictionary alloc]initWithCapacity:1];
    [param setObject:@"addmailingaddress" forKey:@"action"];
    [param setObject:userid forKey:@"user_id"];
    [param setObject:[nameTextField text] forKey:@"mail_people"];
    [param setObject:[addressTextField text] forKey:@"mail_address"];
    [param setObject:[pCodeTextField text] forKey:@"mail_zipcode"];
    [param setObject:[phoneTextField text] forKey:@"mail_phone"];
    [param setObject:[[NSBundle mainBundle] localizedStringForKey:(@"ClientVersion") value:@"" table:nil] forKey:@"version"];
    
    NSDictionary* dic = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:url, @"requestFinished:", nil] forKeys:[NSArray arrayWithObjects:@"url", @"requestMethod", nil]];
    
    [[[ELifeAppDelegate App] httpRequestPoint]openUrl:dic postParam:param target:self];

    [param release];
}

- (void)showAlertView:(NSString *)msg
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提醒" message:msg delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alertView show];
    [alertView release];
}


- (void)requestFinished:(NSData *)response
{
    [[ELifeAppDelegate App] hideProgressDialog];
    
    GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:[GTMBase64 decodeData:response] options:0 error:nil];
    GDataXMLElement* rootElement = [doc rootElement];
    NSLog(@"--------rootElement:%@", rootElement);
    GDataXMLElement* bodyElement = [[rootElement elementsForName:@"body"]objectAtIndex:0];
    NSString* mailId = [[[bodyElement elementsForName:@"mail_id"] objectAtIndex:0] stringValue];
    
    // 成功
    if ([mailId length] > 0) {
        [FlightSearchController getCheckedModel].mail_id = mailId;
        [self backToBookPage];
    } else {
        [self showAlertView:@"添加配送单失败, 请稍后重试"];
    }
    
}

#pragma markend

- (void)save
{
    if ([[nameTextField text] length] == 0
        || [[phoneTextField text] length] == 0
        || [[addressTextField text] length] == 0
        || [[pCodeTextField text] length] == 0) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提醒" message:@"请您将信息填充完整" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertView show];
        [alertView release];
        return;
    }
    
    [self requestMailId];
}

- (void)backToBookPage
{
    if (_addressModel == nil) {
        _addressModel = [[FlightAddressModel alloc] init];
    }
    
    _addressModel.name = nameTextField.text;
    _addressModel.phone = phoneTextField.text;
    _addressModel.address = addressTextField.text;
    _addressModel.pCode = pCodeTextField.text;
    
    [_target performSelector:_backAction withObject:_addressModel];
    [_addressModel release];
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)inputFinished
{
    [nameTextField resignFirstResponder];
    [phoneTextField resignFirstResponder];
    [addressTextField resignFirstResponder];
    [pCodeTextField resignFirstResponder];
}

- (void) registerOver:(NSString *)text
{
    [self requestMailId];
}

@end
